package com.itheima.device.service.impl;


import com.itheima.device.dao.DeviceIndexMapper;
import com.itheima.device.dao.DeviceInfoMapper;
import com.itheima.device.dao.DeviceMapper;
import com.itheima.device.pojo.Device;
import com.itheima.device.pojo.DeviceIndex;
import com.itheima.device.pojo.DeviceInfo;
import com.itheima.device.service.DeviceIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.NewThreadAction;

import java.util.List;
import java.util.Map;

@Service
public class DeviceIndexServiceImpl implements DeviceIndexService {

    @Autowired
    private DeviceIndexMapper deviceIndexMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;



    @Override
    public List<DeviceIndex> findAll() {

        return deviceIndexMapper.selectAll();
    }

    @Override
    public void delete(Integer id) {
        //根据设备id删除设备与设备指标
        deviceIndexMapper.deleteIndexById(id);
        //根据设备id删除设备与设备指标的关联关系
        deviceIndexMapper.deleteDeviceAndDeviceIndexRel(id);

    }

    /**
     * 新增设备和设备指标,说明书
     * @param device
     */
    @Override
    public void add(Device device) {
        deviceMapper.insertSelective(device);
        Integer id = device.getId();
        DeviceInfo deviceInfo = device.getDeviceInfo();
        deviceInfoMapper.insertSelective(deviceInfo);
        deviceInfo.setDid(id);
        Integer[] iids = device.getIndex();
        for (Integer iid : iids) {
            deviceIndexMapper.inserToDeviceIndex(device.getId(), iid);
        }
    }


}
